package com.geping.etl.common.controller;

import com.geping.etl.common.dto.KettleRunResultDto;
import com.geping.etl.common.service.kettle.RunPathFacadeService;
import com.geping.etl.config.SysCodeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Objects;

/**
 * @Author pengcheng.peng 执行脚本服务
 * @Date 2021/7/29 15:43
 * @Version 1.0
 */
@RestController
@Slf4j
public class RunPathController {

    @Autowired
    private RunPathFacadeService runPathFacadeService;

    @GetMapping(value = "/runPath")
    public KettleRunResultDto runPath(HttpServletRequest request, HttpServletResponse response) {
        KettleRunResultDto kettleRunResultDto = null;
            response.setContentType("application/json");
            String pathname = request.getParameter("pathname");
            String dataDate = request.getParameter("dataDate");

            if(Objects.isNull(pathname)){
                kettleRunResultDto = new KettleRunResultDto().error(SysCodeConfig.pathError,"文件路径不能为空!");
            }else{
                File file = new File(pathname.replaceAll("\\\\", "/"));
                if(file.exists()) {
                    kettleRunResultDto = runPathFacadeService.runPath(pathname.replaceAll("\\\\", "/"),dataDate);
                }else {
                    kettleRunResultDto = new KettleRunResultDto().error(SysCodeConfig.pathError,"文件路径不存在!");
                }
            }
            return kettleRunResultDto;
    }
}
